package com.lantosec.hmrp.modules.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.system.entity.SysWorkday;
import com.lantosec.hmrp.modules.system.service.ISysWorkdayService;
import com.lantosec.hmrp.modules.system.vo.SysWorkdayVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 *
 * 系统工作日管理 控制器
 *
 * @author AutoCode
 * @since 2020-03-24
 */
@Api(value = "系统工作日管理控制器", tags = "系统工作日管理控制器")
@RestController
@RequestMapping("/system/workday")
@Log4j2
public class SysWorkdayController extends BaseController {
    @Autowired
    private ISysWorkdayService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<SysWorkdayVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<Page<SysWorkdayVO>> page(@ApiParam(value = "分页对象", required = true) Page<SysWorkday> page, SysWorkdayVO condition){
        page = service.page(page,
                new QueryWrapper<SysWorkday>()
//                        .like(StringUtils.isNotEmpty(condition.getUsername()),"username",condition.getUsername())
        );
        return Result.ok(entity2VO(page));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<SysWorkdayVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ApiOperation(value = "新增数据")
    public Result<String> create(@ApiParam(value = "系统工作日管理VO对象", required = true) @RequestBody @Validated SysWorkdayVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        SysWorkday entity = vo.convertToEntity();
        service.create(entity);
        return Result.ok(entity.getWorkdayDate());
    }

    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "系统工作日管理VO对象", required = true) @RequestBody SysWorkdayVO vo){
        SysWorkday entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @GetMapping("getFrontWorkDay")
    @ApiOperation(value = "查询指定日期后的第N天的工作日是哪天")
    public Result<String> getFrontWorkDay(@ApiParam(value = "指定日期", required = true) @RequestParam(name="currentDay") String currentDay,
                                     @ApiParam(value = "N个工作日", required = true) @RequestParam(name="num") Integer num){
        String workDay = service.getWorkDay(currentDay,num);
        return Result.ok(workDay);
    }

    @GetMapping("getAfterWorkDay")
    @ApiOperation(value = "查询指定日期前的第N天的工作日是哪天")
    public Result<String> getAfterWorkDay(@ApiParam(value = "指定日期", required = true) @RequestParam(name="currentDay") String currentDay,
                                     @ApiParam(value = "N个工作日", required = true) @RequestParam(name="num") Integer num){
        String workDay = service.getAfterWorkDay(currentDay,num);
        return Result.ok(workDay);
    }

    @GetMapping("getWorkDayOfMonth")
    @ApiOperation(value = "查询指定月份的工作日天数")
    public Result<Integer> getWorkDayOfMonth(@ApiParam(value = "指定月份（yyyy-MM）", required = true) @RequestParam(name="currentDate") String currentDate){
        Integer workDayNum = service.getWorkDayOfMonth(currentDate);
        return Result.ok(workDayNum);
    }

    @GetMapping("getWorkDayOfYear")
    @ApiOperation(value = "查询指定年的工作日天数")
    public Result<Integer> getWorkDayOfYear(@ApiParam(value = "指定年份（yyyy）", required = true) @RequestParam(name="currentDate") String currentDate){
        Integer workDayNum = service.getWorkDayOfYear(currentDate);
        return Result.ok(workDayNum);
    }

    @GetMapping("getWorkDayNum")
    @ApiOperation(value = "查询两个时间之间的工作日天数")
    public Result<Integer> getWorkDayNum(@ApiParam(value = "指定开始时间（yyyy-MM-dd）", required = true) @RequestParam(name="startDate") String startDate,
                                         @ApiParam(value = "指定结束时间（yyyy-MM-dd）", required = true) @RequestParam(name="endDate") String endDate){
        Integer workDayNum = service.getWorkDayNum(startDate,endDate);
        return Result.ok(workDayNum);
    }
}
